package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


@Service
public class CommentApiImpl implements CommentApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    // 这个方法提供了  点赞 评论 喜欢保存
    @Override
    public Integer save(Comment comment) {
        // 保存comment 对象
        comment.setCreated(System.currentTimeMillis());  // 创建时间
        Publish publish=mongoTemplate.findById(comment.getPublishId(),Publish.class);
        comment.setPublishUserId(publish.getUserId());  //指定动态用户id
        mongoTemplate.save(comment);
        // 对动态数量加一
        Query query=new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update=new Update();
        //判断动态数据类型
        update.inc(comment.getCol(),1);
        mongoTemplate.updateFirst(query ,update,Publish.class);
        // 查询动态的评论数量
        Query countQuery =new Query(Criteria.where("publishId").is(comment.getPublishId()).
                                        and("commentType").is(comment.getCommentType()));
        Long total =mongoTemplate.count(countQuery,Comment.class);
        return  total.intValue();

    }
            //这个方法要提供  点赞  评论  喜欢和删除
    @Override
    public Integer remove(Comment comment) {

        // 删除comment对象
        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
                        .and("userId").is(comment.getUserId())
        );
        mongoTemplate.remove(query, Comment.class);

        //动态数量减一
        Query updateQuery= new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update=new Update();
        //判断动态类型
        update.inc(comment.getCol(), -1);
        mongoTemplate.updateFirst(updateQuery,update,Publish.class);
        //返回comment 数量
        Query countQuery =new Query(
                Criteria.where("publishId").is(comment.getPublishId()).and("commentType").is(comment.getCommentType()));
                Long total =mongoTemplate.count(countQuery,Comment.class);


        return total.intValue();
    }
    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String publishId) {
        // 分页查询评论列表
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(publishId))
                        .and("commentType").is(2)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip(index).limit(pageSize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long counts = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNum, pageSize, counts, commentList);
    }

    @Override
    public PageBeanVo findCommentByPage(Integer pageNum, Integer pageSize, Integer commentType, Long publishUserId) {
            // 1、分页条件查询
            Integer index = (pageNum-1)*pageSize;

            Query query = Query.query(Criteria.where("commentType").is(commentType).and("publishUserId").is(publishUserId))
                    .skip(index).limit(pageSize);

            List<Comment> commentList = mongoTemplate.find(query, Comment.class);

            long counts = mongoTemplate.count(query, Comment.class);

            // 2、返回分页对象
            return new PageBeanVo(pageNum, pageSize, counts, commentList);
        }
    }

